package com.raylu.realtime.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;

/**
 * Description:
 * <p>
 * Create by lucienoz on 2022/4/8.
 * Copyright © 2022 lucienoz. All rights reserved.
 */
public class DateUtils {
    //2022-03-29T15:23:17.785516+0800
    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter
            .ofPattern("yyyy-MM-dd'T'HH:mm:ss.nZ");
    public static Long getEpochMilli(String DateTimeStr){
        LocalDateTime localDateTime = LocalDateTime.parse(DateTimeStr, dateTimeFormatter);
        long aLong = localDateTime.atZone(ZoneId.systemDefault()).getLong(ChronoField.INSTANT_SECONDS);
        return aLong;
    }


    public static void main(String[] args) {
        LocalDateTime parse = LocalDateTime.parse("2022-03-29T15:23:17.785516+0800", dateTimeFormatter);
        System.out.println(parse.atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nZ")));
        Long epochMilli = getEpochMilli("2022-03-29T15:23:17.785516+0800");
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(epochMilli, 0, ZoneOffset.of("+0800"));
        System.out.println(localDateTime.atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nZ")));

    }
}
